//
//  LGReportDetailHisController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/9/29.
//  his 类型的报告

#import "LGReportDetailHisController.h"
//指标趋势
#import "LGInspectChartController.h"
#import "LGHisReportTableHeader.h"
#import "LGHisReportContentCell.h"
#import "LGHisReportTableCell.h"
#import "LGReportDetailFileCell.h"
@interface LGReportDetailHisController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSArray *fileArray;

@end

@implementation LGReportDetailHisController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"报告详情";
    [self.view addSubview:self.tableView];
//    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/api/doctor.Visitrecords/reportdetail" parameters:@{@"record_id":self.recordId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataArray = returnData[@"report_detail"];
            self.fileArray = returnData[@"report_files"];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    //就诊信息不显示
    NSArray *tempArray = dataDic[@"report_detail"];
    NSMutableArray *dataArray = [NSMutableArray array];
    for (NSDictionary *tempDic in tempArray) {
        //table 类型下数组为空则不显示
        BOOL canAdd = YES;
        NSString *type = LGNSString(tempDic[@"type"]);
        if ([type isEqualToString:@"table"]) {
            NSArray *contents = tempDic[@"contents"];
            canAdd = contents.count>0?YES:NO;
        }
        NSString *title = LGNSString(tempDic[@"title"]);
        if (![title isEqualToString:@"就诊信息"] && canAdd == YES) {
            [dataArray addObject:tempDic];
        }
    }
    self.dataArray = dataArray;
    self.fileArray = dataDic[@"files"];
    [self.tableView reloadData];
}

-(void)checkFileInformation:(UIButton *)sender{
    NSInteger index = sender.tag-1000;
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
    controller.navigationView.title = @"检查报告";
    controller.urlStr = LGNSString(self.fileArray[index]);
    [self.navigationController pushViewController:controller animated:YES];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger count = self.fileArray.count>0?1:0;
    return self.dataArray.count+count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section < self.dataArray.count){
        NSString *type = LGNSString(self.dataArray[section][@"type"]);
        if ([type isEqualToString:@"table"]) {
            NSArray *tempArray = self.dataArray[section][@"contents"][0][@"tlists"];
            return tempArray.count;
            
        }else{
            return 1;
        }
    }else{
        return self.fileArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section < self.dataArray.count){
        NSString *type = LGNSString(self.dataArray[indexPath.section][@"type"]);
        if ([type isEqualToString:@"table"]) {
            //table类型
            NSArray *tempArray = self.dataArray[indexPath.section][@"contents"][0][@"tlists"];
            LGHisReportTableCell *tableCell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];
            tableCell.selectionStyle = UITableViewCellSelectionStyleNone;
            tableCell.keyArray = self.dataArray[indexPath.section][@"contents"][0][@"theader"];
            tableCell.dataDic = tempArray[indexPath.row];
            return tableCell;
            
        }else{
            //content类型
            LGHisReportContentCell *contentCell = [tableView dequeueReusableCellWithIdentifier:@"contentCell" forIndexPath:indexPath];
            contentCell.selectionStyle = UITableViewCellSelectionStyleNone;
            contentCell.dataDic = self.dataArray[indexPath.section];
            return contentCell;
        }
    }else{
        LGReportDetailFileCell *fileCell = [tableView dequeueReusableCellWithIdentifier:@"fileCell" forIndexPath:indexPath];
        fileCell.selectionStyle = UITableViewCellSelectionStyleNone;
        fileCell.title = @"报告详情";
        fileCell.checkBtn.tag = indexPath.row+1000;
        [fileCell.checkBtn addTarget:self action:@selector(checkFileInformation:) forControlEvents:UIControlEventTouchUpInside];
        return fileCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *type = LGNSString(self.dataArray[indexPath.section][@"type"]);
    if ([type isEqualToString:@"table"]) {
        //table类型
        NSArray *tempArray = self.dataArray[indexPath.section][@"contents"][0][@"tlists"];
        LGInspectChartController *controller = [[LGInspectChartController alloc]init];
        controller.code = [NSString stringWithFormat:@"LR@@%@",tempArray[indexPath.row][@"t0"]];
        controller.patientNum = LGNSString(self.dataDic[@"registration_no"]);
        controller.patientid = LGNSString(self.dataDic[@"patient_id"]);
        [self.navigationController pushViewController:controller animated:YES];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section < self.dataArray.count){
        NSString *type = LGNSString(self.dataArray[section][@"type"]);
        if ([type isEqualToString:@"table"]) {
            return viewPix(90);
            
        }else{
            return viewPix(50);
        }
    }else if(self.fileArray.count>0){
        return viewPix(50);
        
    }else{
        return 0.01;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(section < self.dataArray.count){
        NSString *type = LGNSString(self.dataArray[section][@"type"]);
        NSString *title = LGNSString(self.dataArray[section][@"title"]);
        if ([type isEqualToString:@"table"]) {
            LGHisReportTableHeader *headerView = [[LGHisReportTableHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(90))];
            headerView.itemArray = self.dataArray[section][@"contents"][0][@"theader"];
            headerView.title = title;
            return headerView;
            
        }else{
            LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
            headView.topMargin = viewPix(8);
            UIButton *titleBtn = [UIButton buttonWithTitle:title titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#1F6CC6" imageName:@""];
            titleBtn.frame = CGRectMake(viewPix(16), viewPix(20), 112, 27);
            [titleBtn setBackgroundImage:[UIImage imageNamed:@"就诊检查-蓝色"] forState:UIControlStateNormal];
            [headView addSubview:titleBtn];
            return headView;
        }
    }else if(self.fileArray.count>0){
        //报告原文
        LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
        headView.topMargin = viewPix(8);
        UIButton *titleBtn = [UIButton buttonWithTitle:@"报告原文" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#1F6CC6" imageName:@""];
        titleBtn.frame = CGRectMake(viewPix(16), viewPix(20), 112, 27);
        [titleBtn setBackgroundImage:[UIImage imageNamed:@"就诊检查-蓝色"] forState:UIControlStateNormal];
        [headView addSubview:titleBtn];
        return headView;
        
    }else{
        return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(16);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(16))];
    footView.bottomMargin = viewPix(8);
    return footView;
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGHisReportContentCell class] forCellReuseIdentifier:@"contentCell"];
        [_tableView registerClass:[LGHisReportTableCell class] forCellReuseIdentifier:@"tableCell"];
        [_tableView registerClass:[LGReportDetailFileCell class] forCellReuseIdentifier:@"fileCell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

@end
